import faiss
import json
import util.Embedding as Embedding
import numpy as np
from tqdm import tqdm


def creat_vecdb(vec, vec_dim, save_path):
    index = faiss.IndexFlatL2(vec_dim)
    index.add(vec)
    faiss.write_index(index, save_path)


def read_vecdb(db_path):
    vec = faiss.read_index(db_path)
    return vec


def save_concepts_emb():
    with open('data/final.json', 'r', encoding='utf-8') as f:
        videos = json.load(f)
        concept_vec = np.empty((0, 1024))
        for video in tqdm(videos, desc="对concepts进行Embedding"):
            concept_emb = Embedding.get_embedding(video['concept'])
            concept_vec = np.vstack((concept_vec, concept_emb))
        creat_vecdb(concept_vec,1024,'data/concepts_emb.faiss')


# save_concepts_emb()